<?php
/**
 * Created by PhpStorm.
 * User: 王松
 * Time: 2017/8/14 17:51
 */

namespace App\Project\Sms;

use GuzzleHttp\Client;
use App\Project\Contract\Notifications\ReservationNotification;

/**
 * 北京佳讯鸿泰科技有限公司 的短信发送
 * Class JxhtSms
 * @package \App\Project\Sms
 */
class JxhtSms
{
    protected $url = 'http://111.206.219.26:8088/sms.aspx';

    protected $param = [
        'action'   => 'send',
        'userid'   => '153',
        'account'  => '科技文献',
        'password' => 'Kjwx2017',
        'sendTime' => '',
        'extno'    => '',
    ];

    /**
     * 发送给定通知
     * @param                                                             $notifiable
     * @param \App\Project\Contract\Notifications\ReservationNotification $notification
     */
    public function send($notifiable, ReservationNotification $notification)
    {
        $this->setContent($notification->message($notifiable));

        // 将通知发送给 $notifiable 实例
        $this->param['mobile'] = $notifiable->phone;

        $client = new Client();

        $result = $client->get($this->getUrl());

        $result = (array)simplexml_load_string($result->getBody()->getContents());

        if (isset($result['message']) && ($result['message'] == 'ok')) {
            $notification->result(true);
        }
    }

    public function setContent($content)
    {
        $this->param['content'] = $content;
    }

    public function getUrl()
    {
        return $this->url . '?' . http_build_query($this->param);
    }
}